#!/bin/sh

testcase_name="$1"

if [ x"$testcase_name" = "x" ]; then
    echo "Usage: $(basename $0) <testcase_name>"
    exit 1
fi

#
# the dir should be mounted when container started
#
testcase_basedir="/testcases"


#
# the dir put the output reports, json and html log 
#
log_basedir="/log"
log_jsonfile=$log_basedir/${testcase_name}.log
log_htmlfile=$log_basedir/${testcase_name}.html

#
# prepare jobs
#
mkdir -p $log_basedir

#
# apply hack files
#
## add delay on receiving result
cp -rf $testcase_basedir/hack/channel.py  \
       /root/.local/lib/python3.6/site-packages/tbot-0.7.0-py3.6.egg/tbot/machine/channel/channel.py
## fix unicode error
cp -rf $testcase_basedir/hack/htmllog.py  \
       /tbot/generators/htmllog.py


#
# start run board testcases
#
/root/.local/bin/tbot --lab $testcase_basedir/lab.py      \
                      --board $testcase_basedir/board.py  \
                      --log $log_jsonfile                 \
                      -T $testcase_basedir/testcases      \
                      $testcase_name


#
# generate html report
#
/usr/bin/python3.6 /tbot/generators/htmllog.py $log_jsonfile > $log_htmlfile


#
# everyone can access the logs
#
chmod 777 $log_basedir/* -R


#
# clean jobs
#
find $testcase_basedir -name "__pycache__" -exec rm -rf {} \; 2> /dev/null
